/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.openrule;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.HashMap;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import com.leadiv.openrule.event.RulerEvent;
import com.leadiv.openrule.event.RulerListener;

/**
 *
 * @author pborrego
 */
public class Main implements RulerListener, PreferenceChangeListener {
    private BaseRuler currentRuler;
    private Ruler horizontal;
    private RulerVert vertical;
    private Preferences userPrefs;

    public Main() {
        boolean isTransparentable = AWTUtilitiesWrapper.isTranslucencySupported(AWTUtilitiesWrapper.TRANSLUCENT);
        java.util.prefs.Preferences prefs = Preferences.userPrefs();
        Image icon = Toolkit.getDefaultToolkit().getImage( Main.class.getResource("/com/leadiv/openrule/gfx/logo.png") );
        UnitProfile p;
        
        // load up the user preferences
        userPrefs = new Preferences(this);

        horizontal = new Ruler();
        vertical = new RulerVert();

        // set up frame's icons...
        horizontal.setIconImage(icon);
        vertical.setIconImage(icon);

        // Listen to whats happening to the rulers
        horizontal.addRulerListener(this);
        vertical.addRulerListener(this);

        // setup ruler's unit type
        p = userPrefs.getProfile(
            prefs.getInt("ruler-unit-type", Preferences.DEFAULT_RULER_UNIT_TYPE)
        );

        horizontal.setUnitProfile(p);
        vertical.setUnitProfile(p);
        
        // display ruler in vertical format by default
        horizontal.setVisible(false);
        vertical.setVisible(true);
        if(prefs.get("ruler-mode", Preferences.DEFAULT_RULER_MODE).equals("horizontal")) {
            // horizontal format
            horizontal.setVisible(true);
            vertical.setVisible(false);
        }

        if(isTransparentable) {
            if (AWTUtilitiesWrapper.isWindowOpaque(horizontal)) {
               AWTUtilitiesWrapper.setWindowOpaque(horizontal, false);
            }

            if (AWTUtilitiesWrapper.isWindowOpaque(vertical)) {
                AWTUtilitiesWrapper.setWindowOpaque(vertical, false);
            }
        }

        horizontal.setTransparencyProfile(prefs.get("transparency-mode", Preferences.DEFAULT_TRANSPARENCY_MODE));
        vertical.setTransparencyProfile(prefs.get("transparency-mode", Preferences.DEFAULT_TRANSPARENCY_MODE));

        // Always on top option
        horizontal.setAlwaysOnTop(false);
        vertical.setAlwaysOnTop(false);
        if(prefs.getBoolean("always-on-top", Preferences.DEFAULT_ALWAYS_ON_TOP)) {
            horizontal.setAlwaysOnTop(true);
            vertical.setAlwaysOnTop(true);
        }

        currentRuler = horizontal;
    }
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }

    public void close(RulerEvent evt) {
        // Closes out the application...
        
        horizontal.setVisible(false);
        horizontal.dispose();
        
        vertical.setVisible(false);
        vertical.dispose();

        userPrefs.setVisible(false);
        userPrefs.dispose();
    }

    public void minimize(RulerEvent evt) {
        // mimimizes the correct ruler...
        currentRuler.setState(Frame.ICONIFIED);
    }

    public void flip(RulerEvent evt) {
        Dimension dh = horizontal.getSize();
        Dimension dv = vertical.getSize();    

        int dx = (int)Math.abs((dv.height - dh.height)/2);
        int dy = (int)Math.abs((dv.width - dh.width)/2);
            
        if(evt.getSource().equals(horizontal))
        {
            Point ph = horizontal.getLocation();
            dv.height = dh.width;
            ph.translate(dx, -dy);
            
            horizontal.setVisible(false);
            vertical.setLocation(ph);
            vertical.setSize(dv);
            vertical.setVisible(true);
            
            currentRuler = vertical;
            return;
        }
        
        Point pv = vertical.getLocation();
        dh.width = dv.height;
        pv.translate(-dx, dy);

        vertical.setVisible(false);    
        horizontal.setLocation(pv);
        horizontal.setSize(dh);
        horizontal.setVisible(true);

        currentRuler = horizontal;
    }

    public void command(RulerEvent evt) {
        if(evt.getEventValue().equals("f"))
        {
            flip(evt);
            return;
        }
        
        if(evt.getEventValue().equals("s"))
        {
            // move the current ruler to where the mouse is
            Point cursorLocation = MouseInfo.getPointerInfo().getLocation();
            
            if(evt.getSource().equals(horizontal))
            {
                cursorLocation.translate(0, -(horizontal.getRulerSize().height - 4));
                horizontal.setLocation(cursorLocation);
                return;
            }
            
            cursorLocation.translate(-(vertical.getSize().width - 8), 0);
            vertical.setLocation(cursorLocation);
            return;
        }
        
        if(evt.getEventValue().equals("p"))
        {
            if(evt.getSource().equals(horizontal))
            {
                Point ptLoc = horizontal.getLocationOnScreen();
                ptLoc.translate(0, horizontal.getSize().height);

                userPrefs.setLocation(ptLoc);
                userPrefs.pack();
                userPrefs.setVisible(true);

                return;
            }

            Point ptLoc = vertical.getLocationOnScreen();
            ptLoc.translate(vertical.getSize().width, 0);

            
            userPrefs.setLocation(ptLoc);
            userPrefs.pack();
            userPrefs.setVisible(true);
            return;
        }
        
        if(evt.getEventValue().equals("a"))
        {
            if(evt.getSource().equals(horizontal))
            {
                Point ptLoc = horizontal.getLocationOnScreen();
                ptLoc.translate(0, horizontal.getSize().height);
                
                userPrefs.setLocation(ptLoc);
                userPrefs.showTab(Preferences.ABOUT_TAB);
                userPrefs.pack();
                userPrefs.setVisible(true);

                return;
            }

            Point ptLoc = vertical.getLocationOnScreen();
            ptLoc.translate(vertical.getSize().width, 0);

            userPrefs.setLocation(ptLoc);
            userPrefs.showTab(Preferences.ABOUT_TAB);
            userPrefs.pack();
            userPrefs.setVisible(true);
            return;
        }
    }

    public void nudge(RulerEvent evt) {
        Point ptTranslation;
        String vntVal = evt.getEventValue();
        Integer skip = evt.getEventExtras().get("skip") == null ?
            new Integer(1) : new Integer((String)(evt.getEventExtras().get("skip")));

        HashMap<String, Point> translationMap = new HashMap<String, Point>();
        translationMap.put("up", new Point(0, -skip.intValue()));
        translationMap.put("down", new Point(0, skip.intValue()));
        translationMap.put("left", new Point(-skip.intValue(), 0));
        translationMap.put("right", new Point(skip.intValue(), 0));
        translationMap.put("none", new Point(0, 0));

        ptTranslation = translationMap.get(vntVal) != null ?
            translationMap.get(vntVal) : translationMap.get("none");

        Point ptLoc = currentRuler.getLocation();
        ptLoc.translate(ptTranslation.x, ptTranslation.y);
        currentRuler.setLocation(ptLoc);
    }

    public void preferenceChange(PreferenceChangeEvent evt) {
        String k = evt.getKey();

        if(k.equals("ruler-color")) {
            Color c = Utilities.hstoc(evt.getNewValue());
            horizontal.setRulerColor(c);
            vertical.setRulerColor(c);
        }

        if(k.equals("transparency-mode")) {
            horizontal.setTransparencyProfile(evt.getNewValue());
            vertical.setTransparencyProfile(evt.getNewValue());
        }

        if(k.equals("always-on-top")) {
            horizontal.setAlwaysOnTop(evt.getNode().getBoolean(k, Preferences.DEFAULT_ALWAYS_ON_TOP));
            vertical.setAlwaysOnTop(evt.getNode().getBoolean(k, Preferences.DEFAULT_ALWAYS_ON_TOP));
        }

        if(k.equals("ruler-unit-type")) {
            UnitProfile p;
            java.util.prefs.Preferences prefs = Preferences.userPrefs();
            p = userPrefs.getProfile(
                prefs.getInt("ruler-unit-type", Preferences.DEFAULT_RULER_UNIT_TYPE)
            );

            horizontal.setUnitProfile(p);
            vertical.setUnitProfile(p);
        }
    }
}
